using System;
using System.Windows.Forms;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using TXLooker.WorkItemExtention.Core;
using TXLooker.WorkItemExtention.Properties;

namespace TXLooker.WorkItemExtention.UI.QueryManager
{
    public partial class RenameQueryForm : Form
    {
        private IQueryManagerProcessor m_queryManagerProcessor;
        private bool m_bOk;

        public bool OK
        {
            get { return this.m_bOk; }
            protected set { this.m_bOk = value; }
        }
        private RenameQueryForm()
        {
            InitializeComponent();
        }

        public RenameQueryForm(IQueryManagerProcessor queryManagerProcessor)
            : this()
        {
            this.QueryManagerProcessor = queryManagerProcessor;
            this.OldName = this.QueryManagerProcessor.Query.QueryScope == QueryScope.Public ? string.Format(Resources.STRING_TABPAGE_TEAM_QUERIES_NAME, WorkItemExtentionGlobal.GetProject(this.QueryManagerProcessor.SelectedObject).Name, this.QueryManagerProcessor.Query.Name) : string.Format(Resources.STRING_TABPAGE_MY_QUERIES_NAME, WorkItemExtentionGlobal.GetProject(this.QueryManagerProcessor.SelectedObject).Name, this.QueryManagerProcessor.Query.Name);
        }

        private IQueryManagerProcessor QueryManagerProcessor
        {
            get { return this.m_queryManagerProcessor; }
            set
            {
                if (object.ReferenceEquals(null, value))
                    throw new NullReferenceException();
                this.m_queryManagerProcessor = value;
            }
        }

        private void m_btnOk_Click(object sender, EventArgs e)
        {
            WIStoredQuery item = this.QueryManagerProcessor.SelectedObject as WIStoredQuery;
            string queryName = this.m_tbQueryName.Text.Trim(' ');
            //if (queryName == this.QueryManagerProcessor.Query.Name)
            //{
            //    MessageBox.Show(this,TeamXpand.WorkItemExtention.Properties.Resources.STRING_ENTER_NEW_QUERY_NAME,TeamXpand.WorkItemExtention.Properties.Resources.UI_QUERY_MANAGER_CAPTION,MessageBoxButtons.OK,MessageBoxIcon.Stop);
            //    return;
            //}
            try
            {
                this.QueryManagerProcessor.Query.Name = queryName;
                this.QueryManagerProcessor.Query.Update();
                this.OK = true;
                this.NewName = QueryManagerProcessor.Query.QueryScope == QueryScope.Public ? string.Format(Resources.STRING_TABPAGE_TEAM_QUERIES_NAME, WorkItemExtentionGlobal.GetProject(this.QueryManagerProcessor.SelectedObject).Name, queryName) : string.Format(Resources.STRING_TABPAGE_MY_QUERIES_NAME, WorkItemExtentionGlobal.GetProject(this.QueryManagerProcessor.SelectedObject).Name, queryName);//queryName;
                this.Close();

            }
            catch (UnauthorizedAccessException ex)
            {
                MessageBox.Show(this,ex.Message,Resources.UI_QUERY_MANAGER_CAPTION,MessageBoxButtons.OK,MessageBoxIcon.Stop);
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, Resources.UI_QUERY_MANAGER_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }
        }

        private string m_oldName;
        public string OldName
        {
            get { return this.m_oldName; }
            set { m_oldName = value; }
        }

        private string m_newName;
        public string NewName
        {
            get { return this.m_newName; }
            set { this.m_newName = value; }
        }

        private void OnLoad(object sender, EventArgs e)
        {
            this.m_tbQueryName.SelectedText = this.QueryManagerProcessor.Query.Name;
        }

        private void m_btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}